module PCLink
  class Encoder

    def encode_short(i)
      a = []
      a << i
      s = a.pack("v")
      i = s[0] << 8
      i |= s[1]
      return i
    end
    alias_method :decode_short, :encode_short

    def decode_ip(enc_ip)
      ip = []
      i = enc_ip
      until i == 0
        ip << (i & 0xFF)
        i >>=  8  
      end
      return ip.join(".")
    end

    def encode_ip(ip)
      a = ip.split(/\./)
      a = a.collect {|i| i.to_i}
      ip = a[0] + a[1]*0x100 + a[2]*0x10000 + a[3]*0x1000000
      ip
    end
  end
end